﻿Public Class Form1

	Dim WithEvents ColorSelection As New ColorSelection

	Protected Overrides Sub OnLoad(e As System.EventArgs)
		MyBase.OnLoad(e)
		Sheet.NewSheet()
		Sheet.Editor.Parent = Me
		Sheet.Editor.BringToFront()
		Sheet.Editor.Location = New Point(5, FormulaBar.Top + 4)
		Sheet.Editor.Width = ClientSize.Width - Sheet.Editor.Left - 1
		Sheet.Editor.Anchor = AnchorStyles.Left + AnchorStyles.Right + AnchorStyles.Top
	End Sub

	Public Function IsCell(Text As String) As Boolean
		Dim p1 = "", p2 = ""
		For i = 1 To Len(Text)
			Dim c = Text(i - 1)
			If c >= "A" And c <= "Z" Then
				If p2 <> "" Then Return False
				p1 &= c
			End If
			If c >= "0" And c <= "9" Then
				If p1 = "" Then Return False
				p2 &= c
			End If
		Next
		Return True
	End Function

	Public Function GetCell(Text As String) As Cell
		Dim p1 = "", p2 = ""
		For i = 1 To Len(Text)
			Dim c = Text(i - 1)
			If c >= "A" And c <= "Z" Then
				If p2 <> "" Then Return Nothing
				p1 &= c
			End If
			If c >= "0" And c <= "9" Then
				If p1 = "" Then Return Nothing
				p2 &= c
			End If
		Next
		Dim x = 0
		For i = 1 To Len(p1)
			x = 26 * x + Asc(p1(i - 1)) - 64
		Next
		Return Sheet.Cells(x, p2)
	End Function

	Private Sub MnuNew_Click(sender As System.Object, e As System.EventArgs) Handles MnuNew.Click, BtnNew.Click
		Sheet.NewSheet()
	End Sub

	Dim SaveDialog As New SaveFileDialog
	Private Sub MnuSave_Click(sender As System.Object, e As System.EventArgs) Handles MnuSave.Click, BtnSave.Click
		If Sheet.File = "" Then
			MnuSaveAs.PerformClick()
			Return
		End If
		Sheet.Save()
	End Sub

	Private Sub MnuSaveAs_Click(sender As System.Object, e As System.EventArgs) Handles MnuSaveAs.Click
		SaveDialog.Filter = "*.sps|*.sps"
		If Not SaveDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Return
		Sheet.File = SaveDialog.FileName
		MnuSave.PerformClick()
	End Sub

	Dim OpenDialog As New OpenFileDialog
	Private Sub MnuOpen_Click(sender As System.Object, e As System.EventArgs) Handles MnuOpen.Click, BtnOpen.Click
		OpenDialog.Filter = "*.sps|*.sps"
		If Not OpenDialog.ShowDialog = Windows.Forms.DialogResult.OK Then Return
		Sheet.File = OpenDialog.FileName
		Sheet.Open()
	End Sub

	Private Sub MnuPaste_Click(sender As System.Object, e As System.EventArgs) Handles MnuPaste.Click, BtnPaste.Click
		Sheet.Paste()
	End Sub

	Private Sub MnuClearContent_Click(sender As System.Object, e As System.EventArgs) Handles MnuClearContent.Click
		Sheet.ClearContents()
	End Sub

	Private Sub MnuSelectAll_Click(sender As System.Object, e As System.EventArgs) Handles MnuSelectAll.Click
		Sheet.SelectAll()
	End Sub

	Private Sub MnuExit_Click(sender As System.Object, e As System.EventArgs) Handles MnuExit.Click
		Close()
	End Sub

	Private Sub Colors_ColorClicked(col As System.Drawing.Color) Handles ColorSelection.ColorClicked
		Dim Button As ToolStripSplitButton = ColorSelection.Tag
		If Button Is BtnBackcolor Then
			Sheet.AddRegion("bc", col.AsText)
		Else
			Sheet.AddRegion("fc", col.AsText)
		End If
		Dropdown.Close()
	End Sub

	Private Sub BtnBold_Click(sender As System.Object, e As System.EventArgs) Handles BtnBold.Click, MnuBold.Click
		Sheet.AddRegion("b", If(Sheet.IsBold = "1", "0", "1"))
	End Sub

	Private Sub MnuItalic_Click(sender As System.Object, e As System.EventArgs) Handles MnuItalic.Click
		Sheet.AddRegion("i", If(Sheet.IsItalic = "1", "0", "1"))
	End Sub

	Private Sub Sheet_UndoRedoStatusChanged(Undo As Boolean, Redo As Boolean) Handles Sheet.UndoRedoStatusChanged
		BtnUndo.Enabled = Undo
		BtnRedo.Enabled = Redo
		MnuUndo.Enabled = BtnUndo.Enabled
		MnuRedo.Enabled = BtnRedo.Enabled
	End Sub

	Dim Dropdown As MyDropDown
	Private Sub BtnBackcolor_DropDownOpening(sender As Object, e As System.EventArgs) Handles BtnBackcolor.DropDownOpening, BtnForecolor.DropDownOpening
		Dim Button As ToolStripSplitButton = sender
		'If Not Button.DropDownButtonPressed Then
		ColorSelection.Tag = sender
		Dim Host As New ToolStripControlHost(ColorSelection)
		Host.AutoSize = True
		Host.Padding = Padding.Empty
		Host.Margin = Padding.Empty
		Dropdown = New MyDropDown
		'Dropdown.ShowCheckMargin = False
		'Dropdown.ShowImageMargin = False
		Button.DropDown = Dropdown
		Button.DropDown.Renderer = New Renderer
		Button.DropDown.AutoSize = False
		Dim s = ColorSelection.Size
		Button.DropDown.Size = New Size(s.Width + 4, s.Height + 6)
		Button.DropDown.Items.Add(Host)
		Dropdown = Button.DropDown
		'Dropdown.Show(ToolStrip, Button.Bounds.Left, Button.Bounds.Bottom)
		'Host.Control.Capture = True
		Host.Control.Focus()
		'End If
	End Sub

	Class MyDropDown : Inherits ToolStripDropDownMenu

		Protected Overrides ReadOnly Property DefaultPadding As System.Windows.Forms.Padding
			Get
				Return Padding.Empty
			End Get
		End Property

		Protected Overrides ReadOnly Property DefaultMargin As System.Windows.Forms.Padding
			Get
				Return Padding.Empty
			End Get
		End Property

	End Class
End Class